package com.cswnode;

import net.minidev.json.JSONUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * @author csw
 * @create 2020/10/31 17:50
 */
@SpringBootTest
public class KafkaTest2 {

    @Autowired
    private kafkaProducer kafkaProducer;
    @Test
    public void test(){

        kafkaProducer.sendMessage("test","hello");
        kafkaProducer.sendMessage("test","world");
        kafkaProducer.sendMessage("test","lalalla");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("123");
    }

}

@Component
class kafkaProducer{
    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendMessage(String topic,String content){
        kafkaTemplate.send(topic,content);
    }

}

@Component
class KafkaConsumer{
    @KafkaListener(topics = {"test"})
    public void handleMessage(ConsumerRecord record){
        System.out.println(record.value());
    }

}
